/**
 * Created by zycool on 2016/9/6.
 * 详情 - 涂层维修 - 维修整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    // 项目名称
    var $project_name = $('#JQ_project_name');

    // 详情列表
    var $project_table = $('#JQ_project_table'), DTPRO;

    // 查询对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            data.mode = true;
            data.repairTaskId = function () {
                return $('#JQ_repairTaskId').val();
            }();
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            return data;
        }
    };

    DTPRO = $project_table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(corrctrlApi.url_paint_details(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.defectInfoList.total;
                returnData.recordsFiltered = dataset.result.defectInfoList.total;
                returnData.data = DyyBase.clearNull(dataset.result.defectInfoList.list);
                callback(returnData);

                // 项目名称
                $project_name.html(dataset.result.repairTask.name);
            });
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: 'pipeLineName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'customNumber', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'nagmCustomNumber', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'dbValue', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'faultGrade', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'repairSuggestion_label', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'latestRepairTime', render: function (data) {
                var time = Dyy.getDate(data);
                return '<span title="' + time + '">' + time + '</span>';
            }
            },
            {
                data: 'repairState_label', render: function (data) {
                var color = '';
                switch (true) {
                    case (data == '已修复' || data == '未修复'):
                        color = 'zy-status-gray';
                        break;
                    case (data == '难以修复'):
                        color = 'zy-status-red';
                        break;
                    case (data == '修复中'):
                        color = 'zy-status-blue';
                        break;
                    default:
                        break;
                }
                return '<span title="' + data + '" class="' + color + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i data-id="' + data + '" class="zy-details-icon" title="展开详情"></i>';
            }
            },
            {
                data: 'id', render: function (data, type, row) {
                return '<i data-defectId="' + data + '" data-status="' + row.repairState + '" data-code="' + row.customNumber + '" data-pipe="' + row.pipeLineName + '" class="zy-record-icon" title="维修记录"></i>';
            }
            }
        ]
    });

    // 涂层详情模板
    var paintTemplate = [
        {name: 'pipeLineName', title: '管道名称：'},
        {name: 'customNumber', title: '漏损点编号：'},
        {name: 'locationDescription', title: '位置描述：'},
        {name: 'mileage2', title: '绝对距离(m)：'},
        {name: 'nagmCustomNumber', title: '最近测试桩：'},
        {name: 'nagmMileage2', title: '相对最近测试桩距离(m)：'},
        {name: 'buriedDepth', title: '埋深：'},
        {name: 'dbValue', title: 'dB值：'},
        {name: 'irValue', title: 'IR%：'},
        {name: 'onPotential', title: '通电电位(mV)：'},
        {name: 'offPotential', title: '断电电位(mV)：'},
        {name: 'faultGrade', title: '漏损点等级：'},
        {name: 'acInterference_label', title: '交流干扰：'},
        {name: 'dcInterference_label', title: '直流干扰：'},
        {name: 'repairSuggestion_label', title: '修复建议：'},
        {name: 'latestRepairTime', title: '最迟修复时间：', render: function(d){
            return Dyy.getDate(d);
        }},
        {name: 'repairState_label', title: '修复状态：'},
        {name: 'testingDate', title: '检测日期：', render: function(d){
            return Dyy.getDate(d);
        }},
        {name: 'testingCompany', title: '检测单位：'},
        {name: 'recordTime', title: '记录时间：', render: function(d){
            return Dyy.getDate(d);
        }},
        {name: 'recorder', title: '记录人：'},
        {name: 'remarks', title: '备注：'}
    ];

    // 展开详情
    $project_table.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTPRO, $project_table, paintTemplate);
    });

    // 展开记录
    $project_table.on('click', 'i.zy-record-icon', function () {
        // 当前按钮
        var $this_i = $(this);
        // 维修状态
        var status = $this_i.attr('data-status');
        // 携带的ID
        var defectId = $this_i.attr('data-defectId');
        // 任务编号
        var data_code = $this_i.attr('data-code');
        // 管道名称
        var data_pipe = $this_i.attr('data-pipe');
        // 当前父级 tr
        var $tr = $this_i.parent('td').parent('tr');
        // 当前父级 tbody
        var $tbody = $this_i.parent('td').parent('tr').parent('tbody');
        // 已经存在的动态 tr ?
        var $dy_tr = $tbody.find('tr[data-defectId-repair="' + defectId + '"]');

        // 激活当前按钮选中样式
        $project_table.find('i.zy-record-icon').removeClass('active');
        $project_table.find('i.zy-details-icon').removeClass('active');
        $this_i.addClass('active');

        // 隐藏其他动态行
        $project_table.find('tr.zy-dy-tr').hide();

        // 有则显示 无则填充
        if ($dy_tr.length > 0) {
            $dy_tr.show();
        } else {
            showRepairDetails($tr, defectId, status, data_code, $this_i, data_pipe);
        }
    });

    // 维修记录步骤 - 数字圆圈
    $project_table.delegate('i.zy-num-click', 'click', function () {
        var $this_i = $(this);
        if (!($this_i.hasClass('gray'))) {
            $project_table.find('i.zy-num-click').removeClass('active');
            $this_i.addClass('active');
            var data_node = $this_i.attr('data-node');
            var data_id = $this_i.attr('data-id');
            $project_table.find('tr[data-defectid-repair="' + data_id + '"]').find('div.zy-dy-info').empty().append(recordContent[data_node]);
        }
    });

    // 填充维修记录
    function showRepairDetails(tr, id, status, code, tag, pipe) {

        // 如果是流程完成状态,直接跳转到作业点页面
        if (status === 'ALREADY_REPAIR' || status === 'DIFFICULT_TO_REPAIR') {
            var project_name = $project_name.text();
            var url = DyyBase.root_path + '/corrctrl/repairMgr/toRepairWorkRecord.html?id=' + id + '&name=' + DyyBase.unicodeUrl(project_name) + '&code=' + code + '&pipe=' + DyyBase.unicodeUrl(pipe);
            window.location.href = url;
        }

        DyyBase.ajax(corrctrlApi.url_repair_record_all(), {defectId: id}, function (dataset) {
            if (dataset.bool) {
                recordContent.head = '';
                recordContent.body = '';
                loopNode(dataset.result, id, code, tr, status, tag);
            }
        })
    }

    // 返回列表
    $('#JQ_goback').click(function () {
        window.location.href = DyyBase.root_path + '/corrctrl/CoatRepairTask/init.html';
    });

})(jQuery, window, document);